<?php
/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    const
        ADMIN=1,
        MERCHANT=2,    
        MEMBER=3,
        IS_SOCIAL_LOGIN=999
        ;
    
    public $scenario;
    
    private $_id;
    private $_username = "50a9c7dbf0fa09e8969978317dca12e8";
    private $_password = "0b342b1fcc3f9dcac264138aeacd42bc";
    
    private $_socialProvider;
    
    /**
    * Validates the username and password.
    * This method should check the validity of the provided username
    * and password in some way. In case of any authentication failure,
    * set errorCode and errorMessage with appropriate values and return false.
    * @param string username
    * @param string password
    * @return boolean whether the username and password are valid
    */
    public function authenticate($role=null) {
        $this->scenario=$role==null? self::MEMBER:$role;
        switch($this->scenario){
            case self::ADMIN:
                $user=User::model()->isAdmin()->accessible()->authUsername($this->username)->find();
                break;
            case self::MERCHANT :
                $user=User::model()->isMerchant()->accessible()->authUsername($this->username)->find();
                break;
            case 'social':
                $user=User::model()->accessible()->authSocial(array('provider'=>$this->username, 'identifier'=>$this->password))->find();
                if(!empty($user) && $user->statusCode != Option::STATUS_ACTIVE){
                    $this->errorCode = $user->statusCode;
                    return false;
                }
                break;
            case self::MEMBER:
            default:
                $user=User::model()->accessible()->authUsername($this->username)->find();
                //$user=User::model()->findByAuthLogin($this->username, $this->password);
                if(!empty($user) && $user->isSocialLogin){
                    $this->_socialProvider=$user->getSocialProvider();
                    return $this->errorCode=self::IS_SOCIAL_LOGIN;
                }
                break;
        }//end switch
        
        if (empty($user) || ($role != 'social' && !$user->isPasswordMatch($this->password))) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
            //$this->errorCode = self::ERROR_PASSWORD_INVALID;
        } elseif($user->statusCode != Option::STATUS_ACTIVE){
            $this->errorCode = $user->statusCode;
        }else {
            $this->_id = $user->id;
            //$this->setState('role', $user->roleCode);
            //$this->setState('language', $user->preferredLanguage);

            $auth = Yii::app()->authManager;
            if (!$auth->isAssigned($user->roleCode, $user->id)) {
                if ($auth->assign($user->roleCode, $user->id)) {
                    Yii::app()->authManager->save();
                }
            }
            $this->errorCode = self::ERROR_NONE;
        }
        return $this->errorCode == self::ERROR_NONE;
    }

    public function getId() {
        return $this->_id;
    }

    public function getSocialProvider(){
        return $this->_socialProvider;
    }

    public function getErrorMessage(){
        switch ($this->errorCode) {
            case Option::STATUS_NEW:
                return Lang::t('user.login.flash.fail.not.activated');
                break;
            case Option::STATUS_SUSPENDED:
            case Option::STATUS_DISAPPROVED:
            case Option::STATUS_BLOCKED:
                return Lang::t('user.login.flash.fail.suspended');
                break;
            case Option::STATUS_EXPIRED:
            case Option::STATUS_INACTIVE:
            case Option::STATUS_ARCHIVED:
            case Option::STATUS_DELETED:
                return Lang::t('user.login.flash.fail.expired');
                break;
            case UserIdentity::ERROR_USERNAME_INVALID:
            case UserIdentity::ERROR_PASSWORD_INVALID:
            default:
                switch($this->scenario){
                    case self::MERCHANT:
                        return Lang::t('merchant.login.flash.fail.invalid.login');
                        break;
                    case self::MEMBER:
                    default:
                        return Lang::t('user.login.flash.fail.invalid.login');
                        break;
                }
                break;
        }//end switch
    }
}//end class